package com.bbkb.quartz.util;

import com.bbkb.quartz.vo.page.PageResult;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


public class PageResultUtil {

    public static <R, T> PageResult<R> convert(PageInfo<T> source, Function<T, R> convert) {
        if (source != null) {
            PageResult<R> pageResult = new PageResult<>(source.getPageNum(), source.getPageSize(), source.getTotal());
            List<T> sourceList = source.getList();
            if (CollectionUtils.isNotEmpty(sourceList)) {
                List<R> collect = sourceList.stream().map(convert).collect(Collectors.toList());
                pageResult.setList(collect);
            } else {
                pageResult.setList(Collections.emptyList());
            }
            return pageResult;
        }
        return new PageResult<R>(null);
    }

}
